﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AlphaSortListWinUI
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var tValue = Clipboard.GetText();

            var tArray = tValue.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            var tSortedArray = tArray.ToList();

            tSortedArray.Sort();

            var tStringBuilder = new System.Text.StringBuilder();

            foreach (var tItem in tSortedArray)
            {
                tStringBuilder.AppendLine(tItem);
            }

            Clipboard.SetText(tStringBuilder.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var t = DateTime.Parse("11/10/2010 12:00:00 AM");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var tValue = Clipboard.GetText();

            var tArray = tValue.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            List<string> tSortedArray = tArray.ToList();
            var tCleanedArray = new SortedList<string, string>();

            foreach (var tItem in tSortedArray)
            {
                var tKey = tItem.Trim().ToUpper();
                if (!tCleanedArray.ContainsKey(tKey)) tCleanedArray.Add(tKey, tItem);
            }

            tSortedArray = new List<string>();
            tSortedArray.AddRange(tCleanedArray.Values);
            tSortedArray.Sort();

            var tStringBuilder = new System.Text.StringBuilder();

            foreach (var tItem in tSortedArray)
            {
                tStringBuilder.AppendLine(tItem);
            }

            Clipboard.SetText(tStringBuilder.ToString());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var tValue = Clipboard.GetText();

            var tArray = tValue.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

            List<string> tSortedArray = tArray.ToList();
            var tCleanedArray = new SortedList<string, string>();
            var regex = @"([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)[ \t]+([0-9A-Z]+)";

            foreach (var tItem in tSortedArray)
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(tItem.Trim(), regex))
                {
                    var a = System.Text.RegularExpressions.Regex.Replace(tItem.Trim(), regex, "$1\t$2");
                    var array = a.Split('\t');

                    if (!tCleanedArray.ContainsKey(array[1])) tCleanedArray.Add(array[1], array[0]);
                }
            }
 
            var tStringBuilder = new System.Text.StringBuilder();

            foreach (var tItem in tCleanedArray)
            {
                tStringBuilder.AppendLine(tItem.Value + "\t" + tItem.Key);
            }

            Clipboard.SetText(tStringBuilder.ToString());
        }
    }
}
